package com.atguigu.spzx.manager.service.impl;

import com.atguigu.spzx.common.utils.BeanUtils;
import com.atguigu.spzx.manager.mapper.CategoryBrandMapper;
import com.atguigu.spzx.manager.mapper.CategoryMapper;
import com.atguigu.spzx.manager.service.CategoryBrandService;
import com.atguigu.spzx.manager.entity.CategoryBrand;
import com.atguigu.spzx.manager.vo.brand.BrandQueryRespVo;
import com.atguigu.spzx.manager.vo.categoryBrand.*;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * CategoryBrandServiceImpl
 * </p>
 *
 * @author flyduck
 * @since 2023/10/29
 */
@Service
public class CategoryBrandServiceImpl implements CategoryBrandService {

    @Autowired
    private CategoryBrandMapper categoryBrandMapper;
    @Autowired
    private CategoryMapper categoryMapper;

    /**
     * 条件分页查询
     * @param pageParam
     * @param reqVo
     * @return
     */
    @Override
    public Page<CategoryBrandQueryRespVo> getCategoryBrandPageByQuery(Page<CategoryBrand> pageParam, CategoryBrandQueryReqVo reqVo) {
        return categoryBrandMapper.getCategoryBrandPageByQuery(pageParam,reqVo);
    }

    /**
     * 添加
     * @param reqVo
     * @return
     */
    @Override
    public void saveCategoryBrand(CategoryBrandSaveReqVo reqVo) {
        CategoryBrand categoryBrand = BeanUtils.toBean(reqVo, CategoryBrand.class);
        categoryBrandMapper.insert(categoryBrand);
    }

    /**
     * 修改
     * @param reqVo
     * @return
     */
    @Override
    public void updateCategoryBrandById(CategoryBrandUpdateReqVo reqVo) {
        CategoryBrand categoryBrand = BeanUtils.toBean(reqVo, CategoryBrand.class);
        categoryBrandMapper.updateById(categoryBrand);
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @Override
    public CategoryBrandDetailsRespVo getCategoryBrandById(Long id) {
        CategoryBrand categoryBrand = categoryBrandMapper.selectById(id);
        CategoryBrandDetailsRespVo respVo = BeanUtils.toBean(categoryBrand, CategoryBrandDetailsRespVo.class);
        //获取category的路径
        List<Long> categoryPath = categoryMapper.getCategoryPathById(categoryBrand.getCategoryId());
        respVo.setCategoryPath(categoryPath);
        return respVo;
    }

    /**
     * 根据id删除
     * @param id
     * @return
     */
    @Override
    public void deleteCategoryBrandById(Long id) {
        categoryBrandMapper.deleteById(id);
    }

    /**
     * 根据分类id查询对应的品牌数据
     * @param categoryId
     * @return
     */
    @Override
    public List<BrandQueryRespVo> getBrandListByCategoryId(Long categoryId) {
        return categoryBrandMapper.getBrandListByCategoryId(categoryId);
    }
}
